package com.mumu.poi.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.io.FileOutputStream;

/**
 * @author mumu
 * @version 1.0
 * @date 2024/1/8 16:26
 * @describe
 */
public class AligningGenerate {
    public static void main(String[] args) throws Exception {
        //文件路径
        String filePath = "excel/AligningGenerate.xls";
        //创建Excel文件(Workbook)
        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet("Test");// 创建工作表(Sheet)
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("单元格对齐");
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);//水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        style.setWrapText(true);//自动换行
        style.setIndention((short) 5);//缩进
        style.setRotation((short) 0);//文本旋转，这里的取值是从-90到90，而不是0-180度。
        cell.setCellStyle(style);
        FileOutputStream out = new FileOutputStream(filePath);
        //保存Excel文件
        workbook.write(out);

        out.close();//关闭文件流
        System.out.println("OK!");

    }
}
